﻿

var url = new Url();
var $orderId = url.GetQueryString("orderId");

var loadContractInfo =
{
    //借款类的类别，类别为25，ContractType
    GetDictionaryLoanTypeInfo: function () {

        var perentId = $("#hidContractType").val();

        $.ajax({
            type: "post",
            url: "../Handler/ContractFormationHandler.ashx",
            dataType: "json",
            data: { type: 31, PerentId: perentId, OrderId: $orderId, _: Math.random() },
            async: true,
            cache: true,
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    var model = result.Data;
                    if (model != null) {

                        //清空合同层的菜单
                        $(".right_main .contract_generate .ul12").empty();

                        //清空合同层的菜单下的内容层
                        $(".right_main .contract_generate .ul12_1").empty();

                        $.each(model, function (index, item) {

                            //合同层添加菜单信息
                            $("#ContractLoanType").tmpl(item).appendTo(".right_main .contract_generate .ul12");

                            //合同菜单层添加内容
                            $("#ContractLoanInfoli").tmpl(item).appendTo(".right_main .contract_generate .ul12_1");

                            //为借款协议菜单加一个table标签，用作展示出借人及借款人信息列表
                            if ((perentId == 25 && item.DictValue == 1) || item.DictValue == 7) {
                                $("#ul12_1_" + item.DictValue).before("<table id='hetong_User1'></table>");
                            }
                            ////为委托扣款授权书 菜单添加一个table标签，用作展示出借人及借款人信息列表
                            //if (item.DictValue == 3) {
                            //    $("#ul12_1_" + item.DictValue).before("<table id='hetong_User2'></table>");
                            //}
                        });

                        if (model.length > 6) {
                            $(".right_main .contract_generate .ul12_1").css({ "margin-top": "100px" });
                        }

                        //合同类型菜单的第一个li设置默认样式
                        $(".right_main .contract_generate .ul12 li").eq(0).addClass("li1");

                        //合同生成后的合同标签层样式控制
                        $(".right_main .contract_generate .ul12_1>li:not(:first)").hide();
                        $(".right_main .contract_generate .ul12 li").click(function () {
                            $(this).css({ "background": "#FF612C" }).siblings().css({ "background": "#4ABAE0" });
                            $(".right_main .contract_generate .ul12_1>li").eq($(this).index()).show().siblings().hide();
                        });

                        //如果是所有合同都盖完章后，则可以查看所有的合同

                        for (var showI = 0; showI < model.length; showI++) {
                            //菜单借款协议，添加及展示借款人和出借人列表信息

                            loadContractInfo.showHetongInfo(0, '', model[showI].DictValue, perentId);

                            //if (model[showI].DictValue == 1) {
                            //    //加载借款人及出借人信息
                            //    loadContractInfo.SelectLoadAndfinancingInfo(model[showI].DictValue);
                            //} else {
                            //    //其他菜单直接展示合同信息
                            //    //合同展示
                            //    loadContractInfo.showHetongInfo(0, '', model[showI].DictValue);
                            //}
                        }
                    }
                }
                else {
                    alert(result.Message);
                }
            }
        });
    },
    //动态加载出借人与借款人信息
    SelectLoadAndfinancingInfo: function (typ, perentId) {

        $.ajax({
            type: "post",
            url: "../Handler/ContractFormationHandler.ashx",
            dataType: "json",
            data: { type: 28, OrderId: $orderId, _: Math.random() },
            async: true,
            cache: true,
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    //var s = "夏丽丽-李红,夏丽丽1-李红,夏丽丽2-李红,夏丽丽3-李红,夏丽丽4-李红,夏丽丽5-李红,夏丽丽6-李红";
                    //var ss = s.split(',');

                    var objectStr;  //存储id及出借人和借款人信息
                    var arrList = new Array(); //存数的数组集合

                    var model = result.Data;

                    if (model != null && model.length > 0) {

                        var num = 0;
                        var str = "";

                        if (model.length < 3) {
                            str += "<tr>";
                            for (var i1 = 0; i1 < model.length ; i1++) {
                                str += "<td><a style='cursor: pointer' onclick=\"loadContractInfo.showHetongInfo('" + model[i1][1] + "','" + model[i1][0] + "'," + typ + "," + perentId + ")\">" + model[i1][2] + "-" + model[i1][4] + "</a></td>";
                                objectStr = [model[i1][1], model[i1][2], model[i1][4]];
                                arrList.push(objectStr);
                            }
                            str += "</tr>";
                        }
                        else if (model.length % 3 == 0) {
                            num = model.length / 3;
                            for (var i = 0; i < num; i++) {
                                str += "<tr>";
                                for (var j = i * 3; j < (i + 1) * 3 ; j++) {
                                    str += "<td><a style='cursor: pointer' onclick=\"loadContractInfo.showHetongInfo('" + model[j][1] + "','" + model[j][0] + "'," + typ + "," + perentId + ")\">" + model[j][2] + "-" + model[j][4] + "</a></td>";
                                    objectStr = [model[j][1], model[j][2], model[j][4]];
                                    arrList.push(objectStr);
                                }
                                str += "</tr>";
                            }
                        } else {
                            var num1 = (model.length - (model.length % 3)) / 3;
                            num = num1 + 1;

                            for (var i2 = 0; i2 < num; i2++) {

                                if (i2 == num - 1) {
                                    str += "<tr>";
                                    for (var j1 = num1 * 3 ; j1 < model.length ; j1++) {
                                        str += "<td><a style='cursor: pointer' onclick=\"loadContractInfo.showHetongInfo('" + model[j1][1] + "','" + model[j1][0] + "'," + typ + "," + perentId + ")\">" + model[j1][2] + "-" + model[j1][4] + "</a></td>";

                                        objectStr = [model[j1][1], model[j1][2], model[j1][4]];
                                        arrList.push(objectStr);
                                    }
                                    str += "</tr>";
                                }
                                else {
                                    str += "<tr>";
                                    for (var j2 = i2 * 3; j2 < (i2 + 1) * 3 ; j2++) {
                                        str += "<td><a style='cursor: pointer' onclick=\"loadContractInfo.showHetongInfo('" + model[j2][1] + "','" + model[j2][0] + "'," + typ + "," + perentId + ")\">" + model[j2][2] + "-" + model[j2][4] + "</a></td>";

                                        objectStr = [model[j2][1], model[j2][2], model[j2][4]];
                                        arrList.push(objectStr);
                                    }
                                    str += "</tr>";
                                }
                            }
                        }
                        if ((typ == 1 && perentId == 25) || typ == 7) {
                            $("#hetong_User1").html(str);
                        }
                    } else {
                        loadContractInfo.showHetongInfo(0, '', typ, perentId);
                    }
                }
                else {
                    alert(result.Message);
                }
            }
        });
    },
    //合同展示
    showHetongInfo: function (id, creditListId, typ, perentId) {
        $.ajax({
            type: "post",
            url: "../Handler/ContractFormationHandler.ashx",
            dataType: "json",
            data: { type: 30, OrderId: $orderId, financOrderId: id, creditListId: creditListId, typCode: typ, perentId: perentId, _: Math.random() },
            async: true,
            cache: true,
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    var model = result.Data;
                    if (model != null) {
                        if (typ == "1" && model.length > 1) {
                            $("#ul12_1_" + typ).append("您已经盖章成功了！");

                            var str = "<table>";
                            var num = 0;

                            if (model.length % 2 == 0) {
                                num = model.length / 2;
                                for (var i = 0; i < num; i++) {
                                    str += "<tr>";
                                    for (var j = i * 2; j < (i + 1) * 2 ; j++) {
                                        str += "<td><a href='" + model[j].ContractPath + "' target='_Blank'>" + model[j].ContractName + " 文件</a></td>";
                                    }
                                    str += "</tr>";
                                }
                            } else {
                                var num1 = (model.length - (model.length % 2)) / 2;
                                num = num1 + 1;

                                for (var i2 = 0; i2 < num; i2++) {
                                    if (i2 == num - 1) {
                                        str += "<tr>";
                                        for (var j1 = num1 * 2 ; j1 < model.length ; j1++) {
                                            str += "<td><a href='" + model[j1].ContractPath + "' target='_Blank'>" + model[j1].ContractName + " 文件</a></td>";
                                        }
                                        str += "</tr>";
                                    }
                                    else {
                                        str += "<tr>";
                                        for (var j2 = i2 * 2; j2 < (i2 + 1) * 2 ; j2++) {
                                            str += "<td><a href='" + model[j2].ContractPath + "' target='_Blank'>" + model[j2].ContractName + " 文件</a></td>";
                                        }
                                        str += "</tr>";
                                    }
                                }
                            }

                            str += "</table>";
                            $("#ul12_1_" + typ).append(str);
                        } else {
                            if (model.length == 0 && (typ == "1" || typ == "7")) {
                                //展示订单的出借人信息
                                loadContractInfo.SelectLoadAndfinancingInfo(typ);
                            } else {
                                if (model.length > 0) {
                                    var strUrl = model[0].ContractPath;
                                    var fileName = model[0].ContractName;
                                    $("#ul12_1_" + typ).empty("");

                                    //合同信息展示设置
                                    if (model[0].IsSignature == "1") {
                                        if (typ == "0") {
                                            $("#ul12_1_" + typ).append("您已经授权成功了！<a href='" + strUrl + "' target='_Blank'>点击查看 " + fileName + " 文件</a>");
                                        } else if (typ == "5" || typ == "1") {
                                            $("#ul12_1_" + typ).append("共同借款人已经盖章成功了！<a href='" + strUrl + "' target='_Blank'>点击查看 " + fileName + " 文件</a>");
                                        }
                                        else {
                                            $("#ul12_1_" + typ).append("您已经盖章成功了！<a href='" + strUrl + "' target='_Blank'>点击查看 " + fileName + " 文件</a>");
                                        }
                                    } else {
                                        var myPDF = new PDFObject({ url: strUrl }).embed("ul12_1_" + typ);
                                        if (myPDF == false) {
                                            $("#ul12_1_" + typ).append("您的浏览器不支持pdf文件<a href='" + strUrl + "' target='_Blank'>点击下载  " + fileName + "  文件</a>");
                                        } else {
                                            $("#ul12_1_" + typ).css({
                                                "height": "800px"
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else {
                    alert(result.Message);
                }
            }
        });
    }
}
